import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import AllFile from "../components/AllFile.vue"
import Photo from "../components/Photo.vue"
import Video from "../components/Video.vue"
import Document from "../components/Document.vue"
import Music from "../components/Music.vue"
import Recover from "../components/Recover.vue"
import Share from "../components/Share.vue"
import GetShare from "../components/GetShare.vue"
import UpdateInfo from "../components/UpdateInfo.vue"
import Register from "../components/Register.vue"



Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  { path: '/register',component: Register},
  { path: '/home', component: Home ,
    children: [
    { path: '/allfile', component: AllFile},
    { path: '/photo', component: Photo},
    { path: '/video', component: Video},
    { path: '/document', component: Document},
    { path: '/music', component: Music},
    { path: '/recover', component: Recover},
    { path: '/share', component: Share},
    { path: '/getshare', component: GetShare},
    { path: '/updateinfo', component: UpdateInfo}]}
  ]
const router = new VueRouter({
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login' ||to.path === '/register') return next()
  if(to.path==='/sharefile') {
    window.sessionStorage.setItem('key', to.query.key)
    console.log(to.query.key)
    if(window.sessionStorage.getItem('jti')!= null){
      return next("/getshare")
    }  
  }
  // 获取token
  const jit = window.sessionStorage.getItem('jti')
  if (!jit) return next('/login')
  next()
})

export default router
